package cc.fs.zhihuapp.rest.asynchttp;

import android.util.Log;

import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONObject;


/**
 * Created by fostion on 2015/9/9.
 * 继承AsyncHttpResponseHandler 进一步详细封装
 */
public abstract class ApiHandler extends AsyncHttpResponseHandler {

    private final String TAG = "API_HANDLER_ERRROR";

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        try {
            JSONObject object = new JSONObject(new String(responseBody, "utf-8"));
            onSuccess(object);
        } catch (Exception e) {
            e.printStackTrace();
            onFailure(null);
        }
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
        Log.e(TAG,statusCode +"  "+error.getMessage());
        onFailure(" "+statusCode);
    }

    public abstract void onSuccess(JSONObject object);

    public abstract void onFailure(String error);

}
